package com.kingkim.electromechanical.usercenter.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kingkim.electromechanical.commonutils.model.PageBean;
import com.kingkim.electromechanical.commonutils.util.Common;
import com.kingkim.electromechanical.usercenter.mapper.LoginMapper;
import com.kingkim.electromechanical.usercenter.model.po.User;
import com.kingkim.electromechanical.usercenter.model.vo.UserView;
import com.kingkim.electromechanical.usercenter.service.ILoginService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class LoginServiceImpl implements ILoginService {

    @Resource
    private LoginMapper loginMapper;

    @Override
    public PageBean<UserView> QueryForList(int page, int limit, User user) throws DataAccessException {
        PageHelper.startPage(page,limit);
        List menuList = loginMapper.QueryForList(user);
        PageInfo pi = new PageInfo<User>(menuList);
        PageBean pageBean = new PageBean<User>();
        pageBean.setCurrentPage(pi.getPageNum());
        pageBean.setPageCount(pi.getPages());
        pageBean.setList(pi.getList());
        pageBean.setTotalCount(pi.getTotal());
        return pageBean;
    }

    @Override
    public int DeleteForList(List<String> ids) throws DataAccessException {
        return loginMapper.DeleteForList(ids);
    }

    @Override
    public int SaveOrModify(User user) throws DataAccessException {
        if(StringUtils.isNotBlank(user.getLogin_account_id())){
            return loginMapper.Modify(user);
        }else{
            user.setLogin_account_id(Common.GetUUID());
            user.setCreate_time(Common.DateToString("yyyy-MM-dd hh:mm:ss"));
            return loginMapper.Save(user);
        }
    }
}
